<template>
	<view>
		<view class="uni-container" style="float: left;">
			<span>猜对歌曲奖励金币设置</span>
			<uni-forms ref="form" validateTrigger="bind">
				<uni-forms-item label="最少" name="title" labelWidth="150">
					<input class="uni-input-border" type="number" v-model="caigehongbao.min" style="width: 200px;" />
				</uni-forms-item>
				<uni-forms-item label="最多" name="title" labelWidth="150">
					<input class="uni-input-border" type="number" v-model="caigehongbao.max" style="width: 200px;" />
				</uni-forms-item>

				<view class="uni-button-group pointer">
					<button class="uni-button uni-button-full" type="primary" :disabled="isLoading"
						@click="save">保存</button>
				</view>
			</uni-forms>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isLoading: false,
				"caigehongbao": {
					"min": 0,
					"max": 0,
				}
			}
		},
		mounted() {
			this.getinfo()
		},
		methods: {
			getinfo() {
				this.isLoading = true
				uni.showLoading({
					title: '加载中'
				});
				uniCloud.callFunction({
					name: 'uni-admin',
					data: {
						action: "admin/loadconfig",
						data: {
							id: 'caigehongbao'
						}
					}
				}).then((res) => {
					console.log(res, "res--------------")
					this.isLoading = false
					uni.hideLoading();
					this.caigehongbao = res.result.config
				}).catch((res) => {
					console.log(res)
				}).finally(err => {
					this.isLoading = false
					uni.hideLoading();
				})
			},
			save() {
				if (Number(this.caigehongbao['max']) < Number(this.caigehongbao['min'])) {
					uni.showToast({
						title: "最大值必须大于最小值",
						icon: 'none',
						duration: 2000
					})
					return
				}
				this.isLoading = true
				delete this.caigehongbao._id;
				for (let key in this.caigehongbao) {
					if (key != '_id') {
						this.caigehongbao[key] = Number(this.caigehongbao[key])
					}
				}
				console.log(this.caigehongbao)
				uni.showLoading({
					title: '保存中'
				});
				uniCloud.callFunction({
					name: 'uni-admin',
					data: {
						action: "admin/setconfig",
						data: {
							json: JSON.stringify({
								caigehongbao: this.caigehongbao
							})
						}
					}
				}).then((res) => {
					console.log(res, "res--------------")
					this.isLoading = false
					uni.hideLoading();
					uni.showToast({
						title: "修改成功",
						icon: 'none',
						duration: 2000
					})
				}).catch((res) => {
					console.log(res)
				}).finally(err => {
					this.isLoading = false
					uni.hideLoading();
				})
			}
		}
	}
</script>

<style>

</style>
